package com.shop.service.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.shop.service.module.entity.Result;
import com.shop.service.module.entity.UserEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RequestMapping("/test")
@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello World";
    }

    @GetMapping("/get/json1")
//当返回普通json返回体时
    public Result getJSON1() {
        return Result.end(200, "返回的数据", "请求成功");
    }

    @GetMapping("/get/json2")
//当使用java实体类返回普通json返回体时
    public Result getJSON2() {
        UserEntity u = new UserEntity();
        u.setInsertTime(new Date());
        u.setUsername("admin");
        u.setPassword("123456");
        u.setType(0);
        return Result.end(200, u, "请求成功");
    }

    @GetMapping("/get/json3")
//当使用list对象返回普通json返回体时
    public Result getJSON3() {
        List<UserEntity> list = new ArrayList<>();
        UserEntity u1 = new UserEntity();
        u1.setInsertTime(new Date());
        u1.setUsername("admin");
        u1.setPassword("123456");
        u1.setType(0);

        UserEntity u2 = new UserEntity();
        u2.setInsertTime(new Date());
        u2.setUsername("admin1");
        u2.setPassword("1234561");
        u2.setType(1);
        list.add(u1);
        list.add(u2);
        return Result.end(200, list, "请求成功");
    }

    @GetMapping("/get/json4")
//当使用list对象并将list对象设置成data中的一个属性时
    public Result getJSON4() {
        List<UserEntity> list = new ArrayList<>();
        UserEntity u1 = new UserEntity();
        u1.setInsertTime(new Date());
        u1.setUsername("admin");
        u1.setPassword("123456");
        u1.setType(0);

        UserEntity u2 = new UserEntity();
        u2.setInsertTime(new Date());
        u2.setUsername("admin1");
        u2.setPassword("1234561");
        u2.setType(1);
        list.add(u1);
        list.add(u2);

        JSONObject data = new JSONObject();
        data.put("list", list);

        return Result.end(200, data, "请求成功");
    }

    @GetMapping("/get/json5")
//当使用JSONObject来自定义返回对象时
    public Result getJSON5() {
        JSONObject data = new JSONObject();
        data.put("list", "list");
        data.put("page", "page");

        return Result.end(200, data, "请求成功");
    }


}
